<?php

class RegionHelper extends Helper {

    private $check_rule;
    private $_rgfield;

    public function __construct() {
        $this->check_rule = array(
            'rg_code' => array('require', '大区编号不能为空'),
            'rg_name' => array('require', '大区名称不能为空'),
            'rg_province' => array('require', '省份不能为空'),
            'rg_city' => array('require', '城市名称不能为空')
        );
        $this->_rgfield = array('rg_id', 'rg_code', 'rg_name', 'rg_province', 'rg_city');
    }

    /**
     * 大区数据保存
     * @param type $data
     * @param type $where
     * @return type
     */
    public function regionSave($data, $where = '') {
        if(is_numeric($where)){
            $where = array('`rg_id` = ?' => $where);
        }
        $regionModel = $this->LoadModel('Public');
        $validator = Validator::checkRule($data, $this->check_rule);
        if (!$validator['pass']) {
            return array('success' => false, 'msg' => $validator['msg']);
        }
        if (empty($where)) {
            $id = $regionModel->save('region', $data);
            if ($id) {
                return array('success' => true, 'msg' => $id);
            } else {
                return array('success' => false, 'msg' => '保存失败');
            }
        } else {
            $regionModel->update('region', $data, $where);
            return array('success' => true);
        }
    }

    /**
     * 分页
     * @param type $where
     * @param type $page
     * @param type $limit
     * @param type $param
     * @return type
     */
    public function regionList($where, $page = 1, $limit = PAGE_SIZE, $param = array()) {
        $regionModel = $this->LoadModel('Public');
        $data['count'] = $regionModel->getRowCount('region', $where);
        if (!empty($data['count'])) {
            $data['all'] = $regionModel->getList('region', $this->_rgfield, $where, array($page, $limit), NULL, array('rg_id DESC'));
            foreach($data['all'] AS &$row){
                $uwhere = array(
                    '`u_status` = ?' => 'ok',
                    '`u_dp_type` = ?' => 'rg',
                    '`u_dp_id` = ?' => $row['rg_id'],
                    '`u_scope` = ?' => '部门级'
                );
                $row['rg_mainuser'] = $regionModel->getRowCount('member', $uwhere);
                if($row['rg_mainuser']==1){
                    $row['rg_mainuser'] = $regionModel->getOneRow('member', array('u_realname'), $uwhere);
                    $row['rg_mainuser'] = $row['rg_mainuser']['u_realname'];
                }
            }
            Pagination::SetUrl($Param);
            $data['page'] = Pagination::GetHtml($limit, $page, $data['count']);
        }
        return $data;
    }

    /**
     * 单条数据
     * @param type $where
     * @return type
     */
    public function regionRow($where) {
        if (is_numeric($where)) {
            $where = array('`rg_id` = ?' => $where);
        }
        $regionModel = $this->LoadModel('Public');
        $data = $regionModel->getOneRow('region', $this->_rgfield, $where);
        return $data;
    }
    /**
     * 所有数据
     * @return type
     */
    public function regionAll($where){
        $model = $this->LoadModel('Public');
        $data = $model->getList('region', $this->_rgfield, $where);
        return $data;
    }
    
    /**
     * 删除
     * @param type $where
     * @return type
     */
    public function regionRemove($where){
        if(is_numeric($where)){
            $where = array('`rg_id` = ?' => $where);
        }
        $departmentModel = $this->LoadModel('Public'); 
        $do = $departmentModel->update('region', array('rg_status' => 'del'), $where);
        return $do;
    }
    
}